// 商业贷款计算器
// 商业贷款计算器计算
function houseLoanCalc(){
// 按月计算贷款赔付金额
	var ways= document.getElementById("ways");//还款方式
	var year= document.getElementById("year");//贷款年限
	var amount = document.getElementById("amount");//贷款金额
	var com_rate = document.getElementById("com_rate");//贷款利率
	var yuehuank = document.getElementById("yuehuank");//月还款用于tr
	var monthly_repayment = document.getElementById("monthly_repayment");//月还款
	var tot_int = document.getElementById("tot_int");//支付的利息
	var tot_expenditure = document.getElementById("tot_expenditure");//还款金额


	var principal = parseFloat(amount.value); //将百分比格式转换成小数格式
	var interest = parseFloat(com_rate.value) / 100 / 12;//年利率转换成月利率
	var payments = parseFloat(year.value) * 12;//将年度赔付转换成月度赔付//还款期数

	var x=Math.pow(1+interest,payments);//进行幂运算
	var monthly=(principal*x*interest)/(x-1);

	if(ways.value == 1){
		// tot_expenditure.value=parseInt(amount.value);

		tot_int.value = ((monthly * payments) - principal).toFixed(2);//支付利息

		tot_expenditure.value=(monthly * payments).toFixed(2);//总还款

		monthly_repayment.value = monthly.toFixed(2);//月还款

	}else{
		yuehuank.style.display = "none";//月还款（principal贷款总额，payments还款期数，interest贷款利率）
		var a = 0;
		var sum =0 ;
		var g ;
		for (i = 1; i< payments;i++){

			p=(principal - a)*interest;
			sum +=(principal - a)*interest;
			y=principal/payments+(principal - a);
			a +=principal/payments;
			q = y - p;
			z=principal-a;
			template = i  + p.toFixed(2) + q.toFixed(2) + y.toFixed(2)+ z.toFixed(2) ;

		}
		int_expenditure = sum;
		tot_int.value = int_expenditure.toFixed(2);
		tot_expenditure.value = (principal + sum).toFixed(2);//总还款;

	}

}

// 商业贷款计算器重置
function houseReset(){
	var year= document.getElementById("year");//贷款年限
	var amount = document.getElementById("amount");//贷款金额
	var com_rate = document.getElementById("com_rate");//贷款利率
	var monthly_repayment = document.getElementById("monthly_repayment");//月还款
	var tot_int = document.getElementById("tot_int");//支付的利息
	var tot_expenditure = document.getElementById("tot_expenditure");//还款金额

	year.value=''//贷款年限
	amount.value= '';//贷款金额
	com_rate.value = '';//贷款利率
	monthly_repayment.value = '';//月还款
	tot_int.value = '';//支付的利息
	tot_expenditure.value = '';//还款金额

}

// 导航栏的隐藏点击显示
$(".personal_center_right_header1").find('span').each(function(index,elem){
	$(this).click(function(){
		$(this).attr('class','personal_center_right_b3').siblings('span').attr('class','');
		switch (index){
			case 0:
				$(".personal_1").show();
				$(".personal_2").hide();
				$(".personal_3").hide();
				break;
			case 1:
				$(".personal_1").hide();
				$(".personal_2").show();
				$(".personal_3").hide();
				break;
			case 2:
				$(".personal_1").hide();
				$(".personal_2").hide();
				$(".personal_3").show();
				break;
			default:
				break;
		}
	});
});



// 提前还款计算器
function jisuan(){
// 按月计算贷款赔付金额
	var amount = document.getElementById("amount");//贷款总额
	var ways = document.getElementById("ways");//原贷款期限
	var com_rate = document.getElementById("com_rate");//贷款利率
	var year_3 = document.getElementById("year_3");//第一次贷款的时间年
	var mothy_3 = document.getElementById("mothy_3");//第一次贷款的时间月
	var year_4 = document.getElementById("year_4");//预计提前还款时间年
	var mothy_4 = document.getElementById("mothy_4");//预计提前还款时间月

	var radio1 = document.getElementsByName("huanying_1");//贷款类型name
	var huanying_1 = document.getElementById("huanying_1");//贷款类型一次提前还清
	var huanying_2 = document.getElementById("huanying_2");//贷款类型部分提前还款
	var text_1 = document.getElementById("text_1");//元(不含当月应还款额)

	var chuli_1 = document.getElementById("chuli_1");//处理方式缩短还款年限，月还款额基本不变
	var chuli_2 = document.getElementById("chuli_2");//处理方式减少月还款额，还款期不变
	var monthly_repayment = document.getElementById("monthly_repayment");//原月还款额
	var monthly_repayment_1 = document.getElementById("monthly_repayment_1");//原最后还款期
	var tot_int = document.getElementById("tot_int");//已还款总额
	var tot_int_1 = document.getElementById("tot_int_1");//已还利息额
	var tot_expenditure = document.getElementById("tot_expenditure");//该月一次还款额
	var tot_expenditure_1 = document.getElementById("tot_expenditure_1");//下月起还款额
	var tot_expenditure_2 = document.getElementById("tot_expenditure_2");//节省利息支出
	var tot_expenditure_3 = document.getElementById("tot_expenditure_3");//新的最后还款期
	var tot_expenditure_4 = document.getElementById("tot_expenditure_4");//计算结果提示

// 原贷款的期限yhkqs()
	var s_yhkqs = parseFloat(ways.value);

// 贷款总额
	var dkzys = parseFloat(amount.value);
	//月利率
	var dklv = parseFloat(com_rate.value) / 100 / 12;

	//已还贷款期数
	var year_1 = parseInt(year_4.value)*12;//预计提前还款日期
	var mothy_1 = parseInt(mothy_4.value);

	var year_2 = parseInt(year_3.value)*12;//第一次还款日期
	var mothy_2 = parseInt(mothy_3.value);

	//原最后还款期 (第一还款的日期year_3，mothy_3)
	var  yhkjssj = Math.floor((year_2 + mothy_2 + s_yhkqs - 2) / 12) ;
	var yhkjssj_1 = (year_2 + mothy_2 + s_yhkqs - 2) % 12 + 1;
	var yhkjssj_2=yhkjssj+'年'+yhkjssj_1+'月';

	//预计提前还款时间 tqhksjn年tqhksjy月
	//预计提前还款日期year_4年mothy_4月-第一还款的日期year_3年mothy_3月
	// yhdkqs = (parseInt(document.tqhdjsq.tqhksjn.value) * 12 + parseInt(document.tqhdjsq.tqhksjy.value)) - (parseInt(document.tqhdjsq.yhksjn.value) * 12 + parseInt(document.tqhdjsq.yhksjy.value));

	var  yhdkqs_1 = year_1 + mothy_1;
	var yhdkqs_2 = year_2 + mothy_2;
	var  yhdkqs = yhdkqs_1 - yhdkqs_2;


//原月还款额（dkzys贷款总额，dklv贷款利率）计算公式
	var x = Math.pow(1+dklv,s_yhkqs);
	var yhk = dkzys * (dklv * x )  / (x - 1);


	//已还款总额（原月还款额yhk，yhdkqs预计提前还款日期）
	var yhdkys = yhk * yhdkqs;

	//yhdkqs预计提前还款日期，dkzys贷款总额，dklv利率
	//原月还款额yhk
	yhlxs = 0;
	yhbjs = 0;
	for (i = 1; i <= yhdkqs; i++) {
		yhlxs = yhlxs + (dkzys - yhbjs) * dklv;
		yhbjs = yhbjs + yhk - (dkzys - yhbjs) * dklv;
	}

	var tqhkys = parseFloat(text_1.value);

	//yhk原月还款额,dkzys贷款总额，yhbjs定义，dklv利率
	var  yhbjs = yhbjs + yhk;
//tqhkys部分还款额text_1元(不含当月应还款额)
	var  byhk = yhk + tqhkys;

// 新的最后还款日期
	var yhbjs_temp = yhbjs + tqhkys;


	for (xdkqs = 0; yhbjs_temp <= dkzys; xdkqs++) {
		yhbjs_temp = yhbjs_temp + yhk - (dkzys - yhbjs_temp) * dklv;
		xdkqs = xdkqs - 1;
		var  xyhk1 = dkzys - yhbjs - tqhkys;
		var  xyhk2 = dklv * Math.pow((1 + dklv), xdkqs);
		var xyhk = xyhk1 * xyhk2 ;
		jslx = yhk * s_yhkqs - yhdkys - byhk - xyhk * xdkqs;
		var xdkjssj1 = Math.floor((year_1+mothy_1+ xdkqs -2)/12);
		var xdkjssj2 =(year_1 + mothy_1 + xdkqs - 2)%12 + 1;
		var xdkjssj = xdkjssj1 +'年'+ xdkjssj2 + '月';

	}




	//ra1贷款的类型
	// tqhkws还款类型,text_1元(不含当月应还款额)
	// ra2处理方式年限
	// if (chuli_1.value.checked) {
	//yhbjs定义，tqhkws还款类型,tqhkys，text_1元(不含当月应还款额)，dkzys贷款总额
	//             yhbjs_temp = yhbjs + tqhkys;
	//             for (xdkqs = 0; yhbjs_temp <= dkzys; xdkqs++)
	// // yhk原月还款额
	//             yhbjs_temp = yhbjs_temp + yhk - (dkzys - yhbjs_temp) * dklv;
	//             xdkqs = xdkqs - 1;
	//             xyhk = (dkzys - yhbjs - tqhkys) * (dklv * Math.pow((1 + dklv), xdkqs)) / (Math.pow((1 + dklv), xdkqs) - 1);
	//             jslx = yhk * s_yhkqs - yhdkys - byhk - xyhk * xdkqs;
	// //
	//             xdkjssj = Math.floor((parseFloat(year_4.value) * 12 + parseFloat(mothy_4.value) + xdkqs - 2) / 12) + '年' + ((parseFloat(year_4.value) * 12 + parseFloat(mothy_4.value) + xdkqs - 2) % 12 + 1) + '月';

	// }
	//          else {
	//              xyhk = (dkzys - yhbjs - tqhkys) * (dklv * Math.pow((1 + dklv), (s_yhkqs - yhdkqs))) / (Math.pow((1 + dklv), (s_yhkqs - yhdkqs)) - 1);
	//              jslx = yhk * s_yhkqs - yhdkys - byhk - xyhk * (s_yhkqs - yhdkqs);
	//              xdkjssj = yhkjssj;
	//          }



	// if (huanying_1[0].checked) {
	//     byhk = (dkzys - yhbjs) * (1 + dklv);
	//     xyhk = 0;
	//     jslx = yhk * s_yhkqs - yhdkys - byhk;
	//     xdkjssj =year_4.value + '年' +mothy_4.value + '月';
	// }

	monthly_repayment.value = yhk.toFixed(2);//原月还款额
	monthly_repayment_1.value = yhkjssj_2;//原最后还款期

	tot_int.value = yhdkys.toFixed(2);//已还款总额
	tot_int_1.value = yhlxs.toFixed(2);//已还利息额

	tot_expenditure.value = byhk.toFixed(2);//该月一次还款额
	tot_expenditure_1.value = xyhk.toFixed(2);//下月起月还款额
	tot_expenditure_2.value = jslx.toFixed(2);//节省利息支出
	tot_expenditure_3.value = xdkjssj;//新的最后还款期



	// tot_expenditure.value = tqhkys.toFixed(2);//该月一次还款额
	// tot_expenditure.value = byhk.toFixed(2);//该月一次还款额
	// tot_expenditure_1.value = xyhk.toFixed(2);//下月起月还款额
	// tot_expenditure_2.value = jslx.toFixed(2);//节省利息支出


}


// 提前还款计算器重置
function reset(){

	var amount = document.getElementById("amount");//贷款总额
	var com_rate = document.getElementById("com_rate");//贷款利率
	var huanying_1 = document.getElementById("huanying_1");//贷款类型一次提前还清
	var huanying_2 = document.getElementById("huanying_2");//贷款类型部分提前还款
	var monthly_repayment = document.getElementById("monthly_repayment");//原月还款额
	var monthly_repayment_1 = document.getElementById("monthly_repayment_1");//原最后还款期
	var tot_int = document.getElementById("tot_int");//已还款总额
	var tot_int_1 = document.getElementById("tot_int_1");//已还利息额
	var tot_expenditure = document.getElementById("tot_expenditure");//该月一次还款额
	var tot_expenditure_1 = document.getElementById("tot_expenditure_1");//下月起还款额
	var tot_expenditure_2 = document.getElementById("tot_expenditure_2");//节省利息支出
	var tot_expenditure_3 = document.getElementById("tot_expenditure_3");//新的最后还款期


	amount.value='';//贷款总额
	com_rate.value='';//贷款利率

	huanying_1.value='';//贷款类型一次提前还清
	huanying_2.value='';//贷款类型部分提前还款
	monthly_repayment.value='';//原月还款额
	monthly_repayment_1.value='';//原最后还款期
	tot_int.value='';//已还款总额
	tot_int_1.value='';//已还利息额
	tot_expenditure.value='';//该月一次还款额
	tot_expenditure_1.value='';//下月起还款额
	tot_expenditure_2.value='';//节省利息支出
	tot_expenditure_3.value='';//新的最后还款期

}




// 说明文档的导航栏的隐藏点击显示
$(".personal_center_right_header1").find('span').each(function(index,elem){
	$(this).click(function(){
		$(this).attr('class','personal_center_right_b3').siblings('span').attr('class','');
		switch (index){
			case 0:
				$(".message1").show();
				$(".message2").hide();
				$(".message3").hide();
				$(".message4").hide();
				break;
			case 1:
				$(".message1").hide();
				$(".message2").show();
				$(".message3").hide();
				$(".message4").hide();
				break;
			case 2:
				$(".message1").hide();
				$(".message2").hide();
				$(".message3").show();
				$(".message4").hide();
				break;
			case 3:
				$(".message1").hide();
				$(".message2").hide();
				$(".message3").hide();
				$(".message4").show();
				break;
			default:
				break;
		}
	});
});